package com.simonking.boot.mcp.client.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.dashscope.utils.JsonUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.simonking.boot.mcp.client.config.NacosPropertity;
import io.modelcontextprotocol.client.McpAsyncClient;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.ChatClient.CallResponseSpec;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * Spring AI
 */
@RestController
public class FooController {

//    @Autowired
//    private   ChatModel chatModel;
//    @Autowired
//    private ToolCallbackProvider toolCallbackProvider;

    /**
     * 聊天模型客户端
     */
    @Autowired
    private  ChatClient dashScopeChatClient;
    @Autowired
    private NacosPropertity nacosPropertity;



//    /**
//     * @Description: 普通的调用
//     *
//     * @Author: ws
//     * @Date: 2025/2/24 16:32
//     **/
//    @GetMapping("/ai/generate")
//    public String generate(@RequestParam(value = "message", defaultValue = "推荐一个公众号") String message) {
//        ChatClient chatClient = ChatClient.builder(ollamaChatModel)
//                .defaultTools(toolCallbackProvider.getToolCallbacks())
//                .build();
//        CallResponseSpec call = chatClient.prompt(message).call();
//        String content = call.content();
//        return content;
//    }

    //    /**
//     * ChatClient 流式调用
//     * @param message
//     * @return
//     */
//    @RequestMapping(value = "/ai/generate2",produces = "text/event-stream")
//    public Flux<String> generate2(@RequestParam(value = "message", defaultValue = "推荐一个公众号") String message) {
//        ChatClient chatClient = ChatClient.builder(ollamaChatModel)
//                .defaultTools(toolCallbackProvider.getToolCallbacks())
//                .build();
//        return chatClient.prompt(message).stream().content();
//
//    }


//    public FooController(ChatClient.Builder chatClientBuilder) {
//        this.dashScopeChatClient = chatClientBuilder
//                .defaultSystem(DEFAULT_PROMPT)
//                // 实现 Chat Memory 的 Advisor
//                // 在使用 Chat Memory 时，需要指定对话 ID，以便 Spring AI 处理上下文。
//                .defaultAdvisors(
//                        new MessageChatMemoryAdvisor(new InMemoryChatMemory())
//                )
//                // 实现 Logger 的 Advisor
//                .defaultAdvisors(
//                        new SimpleLoggerAdvisor()
//                )
//                // 设置 ChatClient 中 ChatModel 的 Options 参数
//                .defaultOptions(
//                        DashScopeChatOptions.builder()
//                                .withTopP(0.7)
//                                .build()
//                )
//                .defaultTools(toolCallbackProvider.getToolCallbacks())
//                .build();
//    }

    /**
     * @Description: 普通的调用
     *
     * @Author: ws
     * @Date: 2025/2/24 16:32
     **/
    @GetMapping("/simple/chat")
    public String generate(@RequestParam(value = "message", defaultValue = "推荐一个公众号") String message) {
        System.out.println(nacosPropertity.getPassword()+"=============");

        CallResponseSpec call = dashScopeChatClient.prompt(message).call();
        String content = call.content();
        System.out.println(content);
        return content;
    }

    /**
     * @Description: 普通的调用
     *
     * @Author: ws
     * @Date: 2025/2/24 16:32
     **/
    @GetMapping("/simple/chat2")
    public String generate2(@RequestParam(value = "message", defaultValue = "推荐一个公众号") String message) {

        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("role","user");
        jsonObject.addProperty("content","你是谁？");
        JsonArray jsonArray=new JsonArray();
        jsonArray.add(jsonObject);
        System.out.println(JsonUtils.toJson(jsonArray));

        CallResponseSpec call = dashScopeChatClient.prompt(JsonUtils.toJson(jsonArray)).call();
        String content = call.content();
        System.out.println(content);
        return content;
    }

    /**
     * ChatClient 流式调用
     * @param message
     * @return
     */
    @RequestMapping(value = "/stream/chat",produces = "text/event-stream")
    public Flux<String> streamChat(@RequestParam(value = "message", defaultValue = "推荐一个公众号") String message) {
        return dashScopeChatClient.prompt(message).stream().content();

    }
}
